package com.ddstation.home_page.controller;

import com.alibaba.fastjson.JSONObject;
import com.ddstation.common.util.DateUtil;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionMapper;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crm.menu.entity.DtInvsManageInstitutionMenuEntity;
import com.ddstation.crm.menu.mapper.DtInvsManageInstitutionMenuMapper;
import com.ddstation.crm.organization_structure.entity.DtInvsOrganizationStructureEntity;
import com.ddstation.crm.organization_structure.mapper.DtInvsOrganizationStructureMapper;
import com.ddstation.crud.entity.DtInvsManageInstitutionEntity;
import com.ddstation.crud.entity.DtInvsManageInstitutionSupervisorEntity;
import com.ddstation.crud.mapper.DtInvsManageInstitutionMapper;
import com.ddstation.crud.mapper.DtInvsManageInstitutionSupervisorMapper;
import com.ddstation.exam.entity.DtInvsChartInfoEntity;
import com.ddstation.exam.mapper.DtInvsChartInfoMapper;
import com.ddstation.his_verify.service.IHisDataCheckForInvsService;
import com.ddstation.home_page.model.HpCommonParModel;
import com.ddstation.home_page.model.MapModel;
import com.ddstation.home_page.service.HomePageService;
import com.ddstation.weather.service.WeatherService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class HomePageController {

	/**
	 * 机构统查跳转
	 * @param
	 *
	 * @return
	 */
	@RequestMapping(value="invs_home_page.do")
	public String institutionReviewInitEvent(
			@RequestParam(required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/institution_review/institution_review";
	}

	/**
	 * 机构自治跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_institution_autonomy.do")
	public String institutionAutonomyInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/institution_autonomy/institution_autonomy";
	}
	/**
	 * 行业自律跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_industry_autonomy.do")
	public String industryAutonomyInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/industry_autonomy/industry_autonomy";
	}
	/**
	 * 政府监管跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_government_vision.do")
	public String governmentVisionInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/government_vision/government_vision";
	}
	/**
	 * 社会监督跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_society_vision.do")
	public String societyVisionInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/society_vision/society_vision";
	}

	/**
	 * 信用评价跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_feedback.do")
	public String feedbackInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/feedback/feedback";
	}
	/**
	 * 预警处置跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_warning_handle.do")
	public String warningHandleInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/warning_handle/warning_handle";
	}
	/**
	 * 决策分析跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_decision_analysis.do")
	public String decisionAnalysisInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/decision_analysis/decision_analysis";
	}
	/**
	 * 系统设置跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_system_setting.do")
	public String systemSettingInitEvent(
			@RequestParam (required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}

		//获取全部权限菜单
		List<DtInvsManageInstitutionMenuEntity> menuList = dtInvsManageInstitutionMenuMapper.selectAllMenu();
		model.addAttribute("menuList",menuList);

		//获取部门列表
		List<DtInvsOrganizationStructureEntity> organizationStructureList = dtInvsOrganizationStructureMapper.select(loginData.getInstitution().getId(),null);
		for(DtInvsOrganizationStructureEntity organizationStructure:organizationStructureList){
			//判断
			if(organizationStructure.getNoJurisdictionCount() == 0){
				//全部权限
				organizationStructure.setJurisdiction("all");
				organizationStructure.setJurisdictionName("全部");
			}else{
				//字符替换
				String jurisdictionName = organizationStructure.getJurisdictionName();
				if(jurisdictionName != null){
					organizationStructure.setJurisdictionName(jurisdictionName.replace(",","、"));
				}else{
					organizationStructure.setJurisdictionName("无");
				}
			}
		}
		model.addAttribute("organizationStructureList",organizationStructureList);
		model.addAttribute("isManager",loginData.getIsManager());

		return "pc/system_setting/system_setting";
	}

	/**
	 * 政府监督 => 行政执法 跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_law_enforcement.do")
	public String lawEnforcementInitEvent(
			@RequestParam String screenState,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		return "pc/law_enforcement/law_enforcement";
	}

	/**
	 * 社会监督 => 二维码监督 跳转
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_qrcode_supervision.do")
	public String qrcodeSupervisionInitEvent(
			@RequestParam String screenState,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		return "pc/qrcode_supervision/qrcode_supervision";
	}

	/**
	 * 机构详细信息
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_institution_details.do")
	public String institutionDetailsInitEvent(
			@RequestParam String screenState,
			@RequestParam String pageId,
			@RequestParam String institutionId,
			@RequestParam String institutionName,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		model.addAttribute("pageId",pageId);
		model.addAttribute("institutionId",institutionId);
		model.addAttribute("institutionName", institutionName);// 地区ID
		return "pc/institution_details/institution_details";
	}
	/**
	 * 机构详细信息
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_autonomy_examination.do")
	public String autonomyExaminationInitEvent(
			@RequestParam String screenState,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		return "pc/autonomy_examination/autonomy_examination";
	}
	/**
	 * 综合应用
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_comprehensive_application.do")
	public String comprehensiveApplicationInitEvent(
			@RequestParam( required = false) String screenState,
			Model model,
			HttpSession session) {
		model.addAttribute("screenState",screenState);
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}
		//获取菜单权限
		List<DtInvsManageInstitutionMenuEntity> jurisdictionMenus = loginData.getJurisdictionMenus();
		for(DtInvsManageInstitutionMenuEntity menu:jurisdictionMenus){
			model.addAttribute(menu.getUrl(),true);
		}
		return "pc/comprehensive_application/comprehensive_application";
	}

	/**
	 * 政府监管 => 行政执法 => 诊疗行为监管
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_prescription_supervision.do")
	public String prescriptionSupervisionInitEvent(
			@RequestParam String screenState,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		return "pc/prescription_supervision/prescription_supervision";
	}

	/**
	 * 机构自制 => 医疗His
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_medical_his.do")
	public String medicalHisInitEvent(
			@RequestParam String screenState,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		return "pc/medical_his/medical_his";
	}

	/**
	 * 政府监管 => 行政执法 => 卫生消毒监管
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_account_supervision.do")
	public String accountSupervisionInitEvent(
			@RequestParam String screenState,
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		model.addAttribute("screenState",screenState);
		return "pc/account_supervision/account_supervision";
	}

	@RequestMapping(value="avoidPwLoad.do")
	public String avoidLoad(HttpSession session) {
		DtInvsManageInstitutionSupervisorEntity doctor = new DtInvsManageInstitutionSupervisorEntity();
		String newLoginUser = "invs_operator";
		doctor.setLoginUser(newLoginUser);
		doctor.setLoginPassword("123456");
		doctor = dtInvsManageInstitutionSupervisorMapper.selectByLoginUser(doctor);
		Map<String, Object> ret = new HashMap<String, Object>();
//		if (doctor == null) { // 用户名密码不匹配
//			ret.put("errorcode", -1);
//			ret.put("errormsg", "用户名or密码不正确");
//			return "登陆失败";
//		} else { // 用户身份验证ok
//			// session中已经存在账号信息
//			if (session != null && session.getId() != null) {
//				SessionLoginData _loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
//				if (_loginData != null && _loginData.getDoctor() != null) {
//					if (!newLoginUser.equals(_loginData.getDoctor().getLoginUser())) { // session中已经存在的账号和现在登录的账号不同
//						ret.put("errorcode", -1);
//						ret.put("errormsg", "1台电脑只能允许登录1个账号");
//						return "1台电脑只能允许登录1个账号";
//					}
//				}
//			}

		// 机构信息
		DtInvsManageInstitutionEntity institution = dtInvsManageInstitutionMapper.selectByPrimaryKey(doctor.getInstitutionId());

		// 登录信息session保存
		SessionLoginData loginData = new SessionLoginData();
		loginData.setDoctor(doctor);
		loginData.setInstitution(institution);
		session.setAttribute(SessionLoginData.KEY, loginData);
		return "pc/home_page/home_page";
	}

	@RequestMapping(value="avoidLoad.do")
	public String avoidLoadForLinan(HttpSession session) {
		DtInvsManageInstitutionSupervisorEntity doctor = new DtInvsManageInstitutionSupervisorEntity();
		String newLoginUser = "invs_operator";
		doctor.setLoginUser(newLoginUser);
		doctor.setLoginPassword("123456");
		doctor = dtInvsManageInstitutionSupervisorMapper.selectByLoginUser(doctor);
		Map<String, Object> ret = new HashMap<String, Object>();
//		if (doctor == null) { // 用户名密码不匹配
//			ret.put("errorcode", -1);
//			ret.put("errormsg", "用户名or密码不正确");
//			return "登陆失败";
//		} else { // 用户身份验证ok
//			// session中已经存在账号信息
//			if (session != null && session.getId() != null) {
//				SessionLoginData _loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
//				if (_loginData != null && _loginData.getDoctor() != null) {
//					if (!newLoginUser.equals(_loginData.getDoctor().getLoginUser())) { // session中已经存在的账号和现在登录的账号不同
//						ret.put("errorcode", -1);
//						ret.put("errormsg", "1台电脑只能允许登录1个账号");
//						return "1台电脑只能允许登录1个账号";
//					}
//				}
//			}

		// 机构信息
		DtInvsManageInstitutionEntity institution = dtInvsManageInstitutionMapper.selectByPrimaryKey(doctor.getInstitutionId());

		// 登录信息session保存
		SessionLoginData loginData = new SessionLoginData();
		loginData.setDoctor(doctor);
		loginData.setInstitution(institution);
		session.setAttribute(SessionLoginData.KEY, loginData);
		return "pc/home_page/home_page";
	}


	/**
	 * 加载地图散点数据
	 * @param datePatten
	 * @param ua
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_home_page.do/load_map", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadHomePageEvent(
			@RequestParam Integer datePatten,  // 0：今日  1：本周
			@RequestParam String ua,  // 浏览器唯一标识
			HttpSession session) {

		log.debug("首页地图数据查询开始");

		Calendar begintime = Calendar.getInstance();

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errcode", -1);
		ret.put("errMsg", "系统出错，请稍后再试！");

		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY );

		String invsInstitutionId=loginData.getInstitution().getId();//登录的卫监机构

		String[] dateRange = cvnDateRange(datePatten);
		HpCommonParModel _p = new HpCommonParModel();
		_p.setInvsInstitutionId(invsInstitutionId);
		_p.setDtFrom(dateRange[0]);
		_p.setDtTo(dateRange[1]);
		_p.setLimit(5);
		_p.setUa(ua);

		SimpleDateFormat sdf= new SimpleDateFormat("yyyyMM");
		Calendar today = Calendar.getInstance();
		String toYm = sdf.format(today.getTime());
		today.add(Calendar.MONTH, -11);
		String fromYm = sdf.format(today.getTime());

		_p.setFromYm(fromYm);//趋势图中的年月
		_p.setToYm(toYm);//趋势图中的年月

		String id = loginData.getInstitution().getId();//机构

		// 散点图数据
		ret.put("badPracticingTown", homePageService.getBadPracticingTown(_p));
		ret.put("homePageData", homePageService.statisticsByType(id, _p));

		ret.put("errcode", 0);
		ret.put("errMsg", "");

		Calendar endTime = Calendar.getInstance();
		long diff = endTime.getTimeInMillis() - begintime.getTimeInMillis();
		//计算秒
		long seconds = (diff % (1000 * 60)) / 1000;
		log.debug("首页地图和图表数据查询结束，耗时："+seconds+"秒");

		return ret;
	}

	/**
	 * 数据查询
	 *
	 * @return
	 */
	@RequestMapping(value="invs_home_page.do/load", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadInstrumentDisinfectListEvent(
			@RequestParam Integer datePatten,  // 0：今日  1：本周
			@RequestParam String ua,  // 浏览器唯一标识
			HttpSession session) {

		log.debug("首页数据查询开始");

		Calendar begintime = Calendar.getInstance();

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errcode", -1);
		ret.put("errMsg", "系统出错，请稍后再试！");

		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY );

		String invsInstitutionId=loginData.getInstitution().getId();//登录的卫监机构

		String[] dateRange = cvnDateRange(datePatten);
		HpCommonParModel _p = new HpCommonParModel();
		_p.setInvsInstitutionId(invsInstitutionId);
		_p.setDtFrom(dateRange[0]);
		_p.setDtTo(dateRange[1]);
		_p.setLimit(5);
		_p.setUa(ua);

		SimpleDateFormat sdf= new SimpleDateFormat("yyyyMM");
		Calendar today = Calendar.getInstance();
		String toYm = sdf.format(today.getTime());
		today.add(Calendar.MONTH, -11);
		String fromYm = sdf.format(today.getTime());

		_p.setFromYm(fromYm);//趋势图中的年月
		_p.setToYm(toYm);//趋势图中的年月
		// 实时监控表格数据查询 [处方违规]
		ret.put("realTimeMonitorList", homePageService.getRealTimeMonitorList(_p.getInvsInstitutionId()));
		// 首页各种统计数据
		//ret.put("hpTotalResModel", homePageService.getVarious(_p));
		// 散点图数据
		//ret.put("badPracticingTown", homePageService.getBadPracticingTown(_p));

		//新首页数据统计
		String id = loginData.getInstitution().getId();//机构

		ret.put("homePageData", homePageService.statisticsByTypeFirst(id, _p));

		ret.put("errcode", 0);
		ret.put("errMsg", "");

		Calendar endTime = Calendar.getInstance();
		long diff = endTime.getTimeInMillis() - begintime.getTimeInMillis();
		//计算秒
		long seconds = (diff % (1000 * 60)) / 1000;
		log.debug("首页数据查询结束，耗时："+seconds+"秒");

		return ret;
	}


	// 根据首页选的区分，转换成查询区间
	private String[] cvnDateRange(int datePatten){

		String[] dateRange = new String[2];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		// 0：今日  1：本周)
		switch (datePatten) {
			//0：今日
			case 0:
				dateRange[0] = sdf.format(cal.getTime());
				dateRange[1] = sdf.format(cal.getTime());
				return dateRange;
			case 1://本周
				dateRange[1] = sdf.format(cal.getTime());
				cal.add(Calendar.DAY_OF_WEEK, -6);
				dateRange[0] = sdf.format(cal.getTime());
				return dateRange;
			case 2:// 本月
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");
				String queryYm = sdf1.format(cal.getTime());
				return DateUtil.getBeginEndDate(queryYm);//根据年月获得 月初、月末日期
			default:
				break;
		}

		return dateRange;
	}


	/**
	 * 天气数据查询[ ]
	 *
	 *  组件：和风天气 [测试：免费   商用：付费 ]
	 *  网址： https://dev.heweather.com/docs/refer/condition
	 *
	 * @return
	 */
	@RequestMapping(value="invs_home_page.do/load_weather", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadWeatherEvent(
			HttpSession session) {

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errcode", -1);
		ret.put("errMsg", "系统出错，请稍后再试！");

		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY );

		ret.put("errcode", 0);
		ret.put("weather", weatherService.getWeather(loginData.getInstitution().getWeatherLocation()));

		return ret;
	}

	/**
	 * 大屏数据
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_home_page.do/load_chart_info", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadChartInfo(
			@RequestParam String id,
			@RequestParam (required=false)Integer datePatten,//0：今日  1：本周  2：本月
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errorcode", -1);
		try {
			if(datePatten != null){
				id = id + "_" + datePatten;
			}

			DtInvsChartInfoEntity dtInvsChartInfoEntity = dtInvsChartInfoMapper.selectOneById(id);
			if(dtInvsChartInfoEntity != null){
				JSONObject chartInfo = JSONObject.parseObject(dtInvsChartInfoEntity.getData());
				ret.put("chartInfo", chartInfo);
				ret.put("errorcode", 0);
			}else{
				ret.put("errormsg", "暂无数据");
			}
		} catch (Exception e) {
			ret.put("errormsg", e.getMessage());
		}

		return ret;
	}

	/**
	 * 综合应用
	 * @param session
	 * @return
	 */
	@RequestMapping(value="invs_home_page.do/load_display_login", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadComprehensiveDisplay(
			@RequestParam Integer datePatten,//1：近一周 3：近一月
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errorcode", -1);

		try {
			String id = "00";
			if(datePatten != null){
				id = id + "_" + datePatten;
			}

			DtInvsChartInfoEntity dtInvsChartInfoEntity = dtInvsChartInfoMapper.selectOneById(id);
			if(dtInvsChartInfoEntity != null){
				JSONObject chartInfo = JSONObject.parseObject(dtInvsChartInfoEntity.getData());
				ret.put("chartInfo", chartInfo);
				ret.put("errorcode", 0);
			}else{
				ret.put("errormsg", "暂无数据");
			}
		} catch (Exception e) {
			ret.put("errormsg", e.getMessage());
		}

		return ret;
	}

	@RequestMapping(value="invs_home_page.do/load_map_info", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadMapInfo(
			@RequestParam Integer institutionType,
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		try {
			SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
			String invsId = loginData.getInstitution().getId();
			List<MapModel> institutionInfoList = dtHealthManageInstitutionMapper.getInstitutionInfoByInstitutionType(invsId, institutionType);
			for(MapModel institutionInfo:institutionInfoList){
				String position = institutionInfo.getPosition();
				if(position != null){
					String[] split = position.split("\\|");
					if(split != null && split.length == 2){
						institutionInfo.setLongitude(Float.parseFloat(split[0]));
						institutionInfo.setLatitude(Float.parseFloat(split[1]));
						institutionInfo.setPosition(null);
					}
				}
			}
			ret.put("mapInfo", institutionInfoList);
			ret.put("errorcode", 0);

		} catch (Exception e) {
			ret.put("errorcode", -1);
			ret.put("errormsg", e.getMessage());
		}

		return ret;
	}

	@RequestMapping(value="invs_home_page.do/load_institution_name", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> loadInstitutionName(
			@RequestParam String searchKey,
			@RequestParam (required=false) Integer street,
			@RequestParam (required=false) Integer industry,
			@RequestParam (required=false) String economic,
			@RequestParam (required=false) Integer level,
			@RequestParam (required=false) Integer ownership,
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		try {
			ret.put("errorcode", 0);
			searchKey = searchKey.replaceAll(" ", "");
			List<HashMap> institutionList = new ArrayList<>();
			if (!StringUtils.isEmpty(searchKey)) {
				HpCommonParModel _p = new HpCommonParModel();
				_p.setProvince(12);
				_p.setCity(208);
				_p.setCounty(1317);
				institutionList = dtHealthManageInstitutionMapper.searchInstitutionsByName(_p, searchKey, street, industry, economic, level, ownership);
				ret.put("institutionList", institutionList);
			}
			ret.put("institutionList", institutionList);

		} catch (Exception e) {
			ret.put("errorcode", -1);
			ret.put("errormsg", e.getMessage());
		}

		return ret;
	}

	@RequestMapping(value="invs_home_page.do/dispaly_search_login", method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> displaySearch(
			@RequestParam String searchKey,
			@RequestParam (required=false) Integer street,
			@RequestParam (required=false) Integer industry,
			@RequestParam (required=false) String economic,
			@RequestParam (required=false) Integer level,
			@RequestParam (required=false) Integer ownership,
			@RequestParam Integer currentPage,
			@RequestParam Integer limit,
			HttpSession session) {
		Map<String, Object> ret = new HashMap<String, Object>();
		try {
			ret.put("errorcode", 0);
			searchKey = searchKey.replaceAll(" ", "");

			HpCommonParModel _p = new HpCommonParModel();
			_p.setProvince(12);
			_p.setCity(208);
			_p.setCounty(1317);
			_p.setLimit(limit);
			if (currentPage > 0) {
				_p.setPageStart((currentPage -1) * limit);
			} else {
				_p.setPageStart(0);
			}

			List<HashMap> institutionList = dtHealthManageInstitutionMapper.searchInstitutionsByName(_p, searchKey, street, industry, economic, level, ownership);
			ret.put("institutionList", institutionList);

			Integer total = dtHealthManageInstitutionMapper.searchInstitutionsCountByName(_p, searchKey, street, industry, economic, level, ownership);
			ret.put("total", total);
		} catch (Exception e) {
			ret.put("errorcode", -1);
			ret.put("errormsg", e.getMessage());
		}

		return ret;
	}

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Inject private HomePageService homePageService;
	@Inject private WeatherService weatherService;
	@Inject private DtInvsManageInstitutionSupervisorMapper dtInvsManageInstitutionSupervisorMapper;
	@Inject private DtInvsManageInstitutionMapper dtInvsManageInstitutionMapper;
	@Inject private DtInvsOrganizationStructureMapper dtInvsOrganizationStructureMapper;
	@Inject private DtInvsManageInstitutionMenuMapper dtInvsManageInstitutionMenuMapper;
	@Inject private DtInvsChartInfoMapper dtInvsChartInfoMapper;
	@Inject private DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper;


	@Inject private IHisDataCheckForInvsService hisDataCheckForInvsService;
}
